

var jrequest = (function() {
  var Authorization = window.localStorage.getItem("auth_token") || '';
  // var Authorization = 'dgfjdlkfjlkds'
	var Jsocket = io('http://localhost:8080');
	// var Jsocket = io('http://192.168.5.128:9900');
	// socket.on()用于接收服务端发来的消息
	Jsocket.on('connect',  () => {
	  console.log('client connect server');
	});
	Jsocket.on('disconnect', () => {
	  console.log('client disconnect');
	});

	return {
		_request(url, method, contentType, params) {
			return new Promise((resolve,reject) => {
				$.ajax({
					headers: {
						Authorization: Authorization
					},
					contentType: contentType,
					dataType: "json",
					data: params,
					// url:`http://1.85.32.5:51244${url}`,
					url: `http://localhost:8080${url}`,
					// url:`http://192.168.5.168:8090${url}`,
					// url:`http://192.168.5.170:8090${url}`,
					type: method,
					success: function(res, textStatus, request) {
						if (res === -2) {
							window.location.href = "login.html";
						} else {
							if (request.getResponseHeader("Authorization")) {
								window.localStorage.setItem("auth_token", request.getResponseHeader("Authorization"))
							}
							let data = {
								data: res,
								total: request.getResponseHeader("X-Total-Count")
							};
							resolve(data)
						}

					}
				})
			})
		},
		get(url,params) {
			return new Promise((resolve,reject) => {
				this._request(url,"get","application/json", params).then(res => {
					if (res) {
						resolve(res)
					}
				})
			})
		},
		post(url,params) {
			return new Promise((resolve,reject) => {
				// this._request(url,"post","application/x-www-form-urlencoded", params).then(res => {
				this._request(url,"post","application/json", JSON.stringify(params)).then(res => {
					if (res) {
						resolve(res)
					}
				})
			})
		},
		put(url,params) {
			return new Promise((resolve,reject) => {
				// this._request(url,"put","application/x-www-form-urlencoded", params).then(res => {
				this._request(url,"put","application/json", JSON.stringify(params)).then(res => {
					if (res) {
						resolve(res)
					}
				})
			})
		},
		delete(url,params) {
			return new Promise((resolve,reject) => {
				this._request(url,"delete","application/json", params).then(res => {
					if (res) {
						resolve(res)
					}
				})
			})
		},
		// socket io
		jsocket(command,params) {
			let socketp = {
				Command: command,
				CommandType: "Business",
				token: Authorization,
				Body: {
				    data :params
				}
			}
			console.log(JSON.stringify(socketp));

			Jsocket.emit('Business', JSON.stringify(socketp));

			return new Promise((resolve,reject) => {
				Jsocket.on('Business',  (data)=>{
					data = JSON.parse(data)
					console.log(data)
					if (command == '56') {
						// 获取证据
						if (data.code == '1') {
							resolve(data)
						} else if (data.code == '2') {
							JPop.alert({msg:'无报警附件',type:'info'})
						}
					} else {
						// 有body值的
						// let body = JSON.parse(data).Body || {}
						// let bodykeys = Object.keys(body)
						// if (bodykeys.length) {
						// 	resolve(JSON.parse(data))
						// }
						// 查岗，督办，公告 下发成功即可
						if (data.Msg) {
			                JPop.alert({msg:data.Msg,type:'success'})
			            }
						resolve(data)
					}

				});
			})

		},
		// socket io
		jsocketTrack(command, params) {
			let socketp = {
				Command: command,
				CommandType: "track",
				token: Authorization,
				Body: {
				    data :params,
				    userId: window.localStorage.getItem("userId") || ''
				}
			};
			console.log(JSON.stringify(socketp));
			Jsocket.emit('track', JSON.stringify(socketp));
			return new Promise((resolve,reject) => {
				Jsocket.on('trackReal',  (data)=>{
					console.log(JSON.parse(data));
					resolve(JSON.parse(data))
				});
			})

		},

		// socket io
		jSocketIo(type,name,command,params) {
			params = params || {}
			let socketp = {
				Command: command,
				CommandType: type,
				token: Authorization,
				Body: {
				    data :params
				}
			}
			console.log(JSON.stringify(socketp))
			Jsocket.emit(type, JSON.stringify(socketp));
			return new Promise((resolve,reject) => {
				Jsocket.on(name,  (data)=>{
					console.log(JSON.parse(data))
					resolve(JSON.parse(data))
				});
			})

		}
	}
}())



